<?php
/**
 * 秒杀活动 api 接口
 * @author maniac
 */
namespace app\api\controller\v1;

use app\facade\Spike AS SpikeFacade;

use app\facade\Goods AS GoodsFacade;

use app\facade\Activity AS ActivityFacade;

class Spike extends Base
{
    /**
     * 获取商品列表
     * @param  int $category_id 商品分类 id
     */
	public function index()
	{
//		$userid = $this->checkUserToken();
		$page       = input('post.page/d', 0);
		
		$limit      = input('post.limit/d', 0);
		// 分页偏移量
		$offset   = ($page - 1) * $limit;
		
		$limit    = $offset.','.$limit;
		
//		$activity = ActivityFacade::activity();
        $activity_id= input('type',5);//类型：5(团购)
		
		$where[]    = [
		
//			['activity_id', 'eq', $activity['id']],
            ['activity_id', 'eq', $activity_id],
            ['pend', '>', time()],
		];
		
		try {
			
			$data = SpikeFacade::spike($where,$limit);
			
        	return j(0, '获取数据成功', $data);
        	
		} catch (\Exception $e) {
			
			return j(1, $e->getMessage());
			
		}
		
    }
    
    /**
     * 商品详情
     */
    public function detail()
    
    {
//		$userid = $this->checkUserToken();

        $post = input('post.');
        
        if (!$post['spike_id']) {
        	
            return j(1, '请选择商品');
        }
        
        $where  = [
        
            'id' => $post['spike_id'],
            
        ];
        
        $spike = SpikeFacade::detail($where);
        
        $wheres  = [
        
        	'id' => $spike['goods_id'],
        	
        ];

        $info = GoodsFacade::detail('', '', $wheres);
        
        $info['promotion_price'] = $spike['promotion_price'];
        
        $info['pend'] = date("Y-m-d H:i:s",$spike['pend']);
        $info['pbegin']=date("Y-m-d H:i:s",$spike['pbegin']);
        $info['spike_num'] = $spike['num'];
        $info['surplus_num'] = $spike['all_num'];
        $info['spike_name'] = $spike['goods_name'];
        $info['spike_describe'] = $spike['describe'];

        $info['goods_detail'] = str_replace('src="/static/', 'src="'.config('system.attach_uri_prefix').'static/', $info['goods_detail']);

        if ($info) {
        	
            return j(0, '获取商品详情成功', $info);
            
        }
        
        return j(1, '获取商品信息失败');
    }
    
}
